$(document).ready(function () {
    var equipment_equipment = '';
    var modify_equipment = '';
    $(".del").click(function (event) {
        event.stopPropagation();
        var equipmentid = $(this).data('equipmentid');
        swal({
                title: "你确定要删除改装备嘛？",
                text: "一旦删除，你将无法恢复这个文件！",
                type: "info",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                closeOnConfirm: false,
                showLoaderOnConfirm: true,
            },
            function () {
                $.ajax({
                        url: 'index.php?c=equipment&a=deleteequipment',
                        type: 'get',
                        dataType: 'json',
                        data: {
                            'equipmentid': equipmentid
                        },
                    }).done(function (data) {
                        if (data.code == 1) {
                            setTimeout(function () {
                                swal({
                                    title: "删除成功!",
                                    type: "success"
                                }, function () {
                                    location.reload();
                                });
                                //  location.reload();
                            }, 1000);

                        } else {
                            setTimeout(function () {
                                swal("删除失败", "", "error"),
                                    function () {
                                        location.reload();
                                    };
                            }, 1000);
                        }
                    })
                    .fail(function (error) {
                        console.log(error);
                    });
            });
    });

    $('.equipment_equipment').on('click', function () {
        $('#file_img').trigger('click');
    });
    //选择图片之后
    $('#file_img').on('change', function () {
        var file = $('#file_img')[0].files[0]; //获取文件都是数组
        //		if(file == undefined) return;
        //		console.log(file);
        var reader = new FileReader();
        //解析完之后进行的函数调用
        reader.onload = function (event) {
            //解析的目标结果
            var dataurl = event.target.result;
            //					console.log(dataurl);
            //进行页面显示海报的图片
            $(".equipment_equipment").attr("src", dataurl);
            equipment_equipment = dataurl;
            //$('.movie_img').css({"background-image":"url("+dataurl+")"});
        }
        //相当于解析图片
        reader.readAsDataURL(file);

    });


    $('.add').click(function () {
        event.stopPropagation();
        var equipment_name = $('.equipment_name').val();
        var equipment_price = $('.equipment_price').val();
        var equipment_property = $('.equipment_property').val();
        if (equipment_equipment === undefined || equipment_equipment.replace(/\s+/g, "") === "") {
            swal("请添加装备图片", "", "info");
        } else if (equipment_name === undefined || equipment_name.replace(/\s+/g, "") === "") {
            swal("请添加装备名称", "", "info");
        } else if (equipment_price === undefined || equipment_price.replace(/\s+/g, "") === "") {
            swal("请添加装备价格", "", "info");
        } else if (equipment_property === undefined || equipment_property.replace(/\s+/g, "") === "") {
            swal("请添加装备属性", "", "info");
        } else {
            //进行参数传递
            var file = $('#file_img')[0].files[0];
            var formdata = new FormData();
            formdata.append('file', file);
            formdata.append('equipment_name', equipment_name);
            formdata.append('equipment_price', equipment_price);
            formdata.append('equipment_property', equipment_property);

            $.ajax({
                    url: 'index.php?c=equipment&a=addequipment',
                    type: 'post',
                    dataType: 'json',
                    processData: false,
                    contentType: false,
                    data: formdata,
                })
                .done(function (data) {
                    if (data.code == 1) {
                        swal("添加成功", "", "success");
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                    } else {
                        swal("添加失败", data.msg, "error");
                    }
                })
                .fail(function (error) {
                    console.log(error);
                });
        }
    });

    //点击头像上传文件
    $('.modify_equipment').on('click', function () {
        $('#modify').trigger('click');
    });
    //选择图片之后
    $('#modify').on('change', function () {
        var file = $('#modify')[0].files[0]; //获取文件都是数组
        var reader = new FileReader();
        //解析完之后进行的函数调用
        reader.onload = function (event) {
            //解析的目标结果
            var dataurl = event.target.result;
            //进行页面显示海报的图片
            $(".modify_equipment").attr("src", dataurl);
            modify_equipment = dataurl;
        }
        //相当于解析图片
        reader.readAsDataURL(file);

    });
    //修改皮肤操作
    $('.modify').click(function (event) {
        var equipment_id = $(this).data('equipmentid');
        var modify_name = $('#' + equipment_id + ' .modify_name').val();
        var modify_price = $('#' + equipment_id + ' .modify_price').val();
        var modify_property = $('#' + equipment_id + ' .modify_property').val();
        //  	console.log(skin_id,modify_id,modify_name,modify_price);
        if (modify_name === undefined || modify_name.replace(/\s+/g, "") === "") {
            swal("请填写装备名称", "", "info");
        } else if (modify_price === undefined || modify_price.replace(/\s+/g, "") === "") {
            swal("请填写装备价格", "", "info");
        }else if (modify_property === undefined || modify_property.replace(/\s+/g, "") === "") {
            swal("请填写装备属性", "", "info");
        }else {
            if(modify_equipment!=undefined) {
                //进行参数传递
                var file = $('#modify')[0].files[0];
                var formdata = new FormData();
                formdata.append('file', file);
                formdata.append('equipment_id', equipment_id);
                formdata.append('modify_property', modify_property);
                formdata.append('modify_name', modify_name);
                formdata.append('modify_price', modify_price);

                $.ajax({
                        url: 'index.php?c=equipment&a=modifyequipment',
                        type: 'post',
                        dataType: 'json',
                        processData: false,
                        contentType: false,
                        data: formdata,
                    })
                    .done(function (data) {
                        if (data.code == 1) {
                            swal("修改成功", "", "success");
                            setTimeout(function () {
                                window.location.reload();
                            }, 1000);
                        } else {
                            swal("修改失败", data.msg, "error");
                        }
                    })
                    .fail(function (error) {
                        console.log(error);
                    });
            } else {
                //如果没有图片传入
                $.ajax({
                        url: 'index.php?c=equipment&a=modifyequipment',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            'equipment_id':equipment_id,
                            'modify_name': modify_name,
                            'modify_price': modify_price,
                            'modify_property':modify_property
                        },
                    }).done(function (data) {
                        if (data.code == 1) {
                            swal("修改成功", "", "success");
                            setTimeout(function () {
                                window.location.reload();
                            }, 1000);

                        } else {
                            swal("修改失败", data.msg, "error");
                        }
                    })
                    .fail(function (error) {
                        console.log(error);
                    });
            }
        }
    });


});
